package com.yc.backstage.service.serviceImpl;

import com.yc.backstage.entity.SysUser;
import com.yc.backstage.entity.SysUserRole;
import com.yc.backstage.mapper.SysUserRoleMapper;
import com.yc.backstage.service.SysUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @Classname SysUserRoleController
* @Description
* @Version 1.0.0
* @Date 2022-08-23 11:49:19
* @Created by Mikechen
*/
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    /**
     * 新增用户与角色管理
     * @param user
     */
    @Override
    public void insertUserRole(SysUser user) {
        Long userId = user.getUserId();
        Long[] roleIds = user.getRoleIds();
        if (StringUtils.isNotEmpty(roleIds)) {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<>(roleIds.length);
            for (Long roleId : roleIds) {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(userId);
                ur.setRoleId(roleId);
                list.add(ur);
            }
            sysUserRoleMapper.batchUserRole(list);
        }
    }

    /**
     * 通过用户ID删除用户和角色关联
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public int deleteUserRoleByUserId(Long userId) {
        return sysUserRoleMapper.deleteUserRoleByUserId(userId);
    }

    /**
     * 批量删除用户和角色关联
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteUserRole(List<Long> ids) {
        return sysUserRoleMapper.deleteUserRole(ids);
    }
}
